
function  start( ... )
	BindCommand()
	AddAIPlayer()
end

function AddAIPlayer( ... )
	-- body
	for i=1,10 do
		if GameServerHelper.HasPlayer(i) == false
			then
			GameServerHelper.AddPlayer(i,"AI"..i)
		end
	end

end

function BindCommand()
	self:BindEvent(LuaNetCommand.Login,OnLogin)
	self:BindEvent(LuaNetCommand.CreateRole,OnCreateRole)
	self:BindEvent(LuaNetCommand.HalfMatch,OnHalfMatch)
end

function ondestroy( ... )
	local  serverData = encode(GameServerHelper.GameServer)
	print("SAVEDATA"..serverData)
	CS.LuaGameTool.SaveData("serverData",serverData)
	-- body
end

function Result( data )
	print("RESU:"..data.command.."data:"..encode(data))
	self:SendToClient(encode(data))
	-- body
end
function  getResult( netdata )
	local  result = {
		state = 1,
		command = netdata.command,
		data = {
		}
	}
	return result
	-- body
end


--100
function  OnLogin( netdata )
	local  data = decode(netdata.data)
	local  result = getResult(netdata)
	local  uuid  = '"'..data.uuid..'"'
	print("OnLogin serverdaga"..encode(GameServerHelper.GameServer))
	
	if GameServerHelper.HasPlayer(data.uuid)
		then 
		result.data = GameServerHelper.GetPlayerInfo(data.uuid)
		result.data.isNewplayer = 0
	else
		result.data.isNewplayer = 1
	end
	Result(result)
	-- body
end


--101
function  OnCreateRole( netdata )
	local  data = decode(netdata.data)
	local  name  = data.name
	local  uuid = data.uuid
	local  result = getResult(netdata)
	local cancreate = GameServerHelper.CheckIsCanAddPlayerByName(name)
	if  cancreate == 0
		then
		result.data = GameServerHelper.AddPlayer(uuid,name)
	else
		result.state = 0
		result.data.error = cancreate
	end
	Result(result)
	-- body
end

--900
function  OnHalfMatch( netdata )
	print("some one want to play half match")
	local  data = decode(netdata.data)
	local  result = getResult(netdata)
	result.data = encode(GameServerHelper.SomeOneEnterHalfMatch(data.uuid))
	Result(result)
	-- body
end